package com.shuwa.controller;

import com.shuwa.pojo.ErrorInf;
import com.shuwa.pojo.Users;
import com.shuwa.service.UserService;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;

@RestController
@RequestMapping("/user")
public class UserController {

    //数据库操作对象
    @Resource
    UserService userService;

    //提示信息对象
    private ErrorInf errorInf=new ErrorInf();


    @RequestMapping(value = "/login", produces = "application/json; charset=utf-8",method= RequestMethod.POST)
    public ErrorInf logcheck(Users user, HttpServletRequest request){
    Users usersql=this.userService.logCheck(user.getUserName());

    if(usersql!=null){
        if((usersql.getUserPassword().equals((user.getUserPassword())))||(usersql.getUserPhone().equals(user.getUserPhone())))
        {
            request.getSession().setAttribute("userid",usersql.getUserID());
            errorInf.setErrcode("0");
            errorInf.setErrmsg("登陆成功");
            System.out.println(request.getSession().getAttribute("userid"));
            return errorInf;
        }else{
            errorInf.setErrcode("40001");
            errorInf.setErrmsg("密码错误");
            return errorInf;
        }
    }else{
        errorInf.setErrcode("40000");
        errorInf.setErrmsg("账户不存在");
        return errorInf;
    }

    }

    @RequestMapping(value = "/unlogged",produces = "application/json; charset=utf-8")
    public ErrorInf unlogged()
    {
        errorInf.setErrcode("40002");
        errorInf.setErrmsg("未登录");
        return errorInf;
    }


    @RequestMapping(value = "/register",produces = "application/json; charset=utf-8",method = RequestMethod.POST)
    public ErrorInf register(Users user){
        boolean existName=userService.checkNameExist(user.getUserName());
        boolean existEmail=userService.checkEmailExist((user.getUserEmail()));
        System.out.println(existEmail);
        if(existName){
            errorInf.setErrcode("50000");
            errorInf.setErrmsg("用户名已存在");
            return errorInf;
        } else if(existEmail){
            errorInf.setErrcode("50002");
            errorInf.setErrmsg("邮箱已注册");
            return errorInf;
        } else {
            boolean eamilFormat=userService.checkEmailFormat(user.getUserEmail());
            if(!eamilFormat)
            {
                errorInf.setErrcode("50003");
                errorInf.setErrmsg("邮箱格式不正确");
                return errorInf;
            }else {
                userService.register(user);
                errorInf.setErrcode("0");
                errorInf.setErrmsg("注册成功");
                return errorInf;
            }
        }
    }



//    @RequestMapping(value = "/logout",method = RequestMethod.POST,produces = "application/json; charset=utf-8")
//    public String logout(HttpServletRequest request){
//            request.getSession().removeAttribute("username");
//        return "{ \"error\":\"已退出\"} ";
//
//    }


}
